# -*- coding: utf-8 -*-

'''
    【简介】
	PySide6中主窗口例子
'''


import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton
from PySide6 import QtCore
from PySide6.QtGui import QIcon, QGuiApplication

# 定义一个MainWindow类，继承自QMainWindow
class MainWindow(QMainWindow):
    # 初始化函数，parent参数可选
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        # 设置窗口标题
        self.setWindowTitle("QMainWindow 例子")
        # 设置窗口大小
        self.resize(800, 400)
        # 获取状态栏
        self.status = self.statusBar()
        # 显示一条消息，延时1000毫秒
        self.status.showMessage("启动中", 1000)
        
        # 创建一个垂直布局
        layout = QVBoxLayout()
        # 创建一个widget，并设置布局
        widget = QWidget(self)
        widget.setLayout(layout)
        # 设置widget的位置和大小
        widget.setGeometry(QtCore.QRect(200, 150, 200, 200))
        #self.setCentralWidget(widget)
        # 将widget赋值给MainWindow的widget属性
        self.widget = widget
        
        # 添加按钮1，点击关闭主窗口
        self.button1 = QPushButton("关闭主窗口")
        self.button1.clicked.connect(self.close)
        layout.addWidget(self.button1)
        
        # 添加按钮2，点击将主窗口居中
        self.button2 = QPushButton("主窗口居中")
        self.button2.clicked.connect(self.center)
        layout.addWidget(self.button2)
        
        # 添加按钮3，点击将主窗口居中
        self.button3 = QPushButton("显示图标")
        self.button3.clicked.connect(lambda :self.setWindowIcon(QIcon("./images/icon.png")))
        layout.addWidget(self.button2)
        
        
    def center(self):
        # 获取屏幕尺寸
        screen = QGuiApplication.primaryScreen().geometry()
        # 获取窗口尺寸
        size = self.geometry()
        # 将窗口居中
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec())
